<?php
class Crc_View_Helper_GridDisplayField extends Zend_View_Helper_Abstract
{
    public $view;

    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
    }
    public function GridDisplayField(\Crc\Grid $grid,
                                     \Crc\Grid\Column $column,
                                     \Crc\Entity\AbstractEntity $item)
    {
        $request = Zend_Controller_Front::getInstance()->getRequest();

        $filter = new Zend_Filter_Word_CamelCaseToDash();

        $path = APPLICATION_PATH
              . DS . 'modules'
              . DS . $request->getModuleName()
              . DS . 'views' . DS. 'scripts' . DS;

        $template =   $request->getControllerName()
                  . DS . $request->getActionName()
                  . DS . 'content-' . $filter->filter($column->getField())
                  . '.phtml';

        if (file_exists($path . $template)) {
            return $this->view->partial($template, array('item' => $item));
        } else {
            $value = $item->get($column->getField());
            if ($value instanceOf \Doctrine\ORM\Proxy\Proxy) {
                $doctrine = \Zend_Registry::get('doctrine');
                $entityManager = $doctrine->getEntityManager();
                $identifier = $entityManager->getUnitOfWork()->getEntityIdentifier($value);
                if (array_key_exists('id', $identifier)) {
                    if ($identifier['id'] == 0) {
                        $value ='';
                    }
                } else {
                    throw new \Exception("Invalid proxy identifier");
                }
            }
            echo '<td class="tab_alb">' . $value . "</td>";
        }
    }

}